package com.feng.controller;

import com.feng.pojo.Emp;
import com.feng.pojo.Result;
import com.feng.service.EmpService;
import com.feng.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LongController {
    @Autowired
    private EmpService empService;
    @RequestMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录：{}",emp);
        Emp e = empService.login(emp);
        if (e != null){
            HashMap<String, Object> claims = new HashMap<>();

            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }else {
            return Result.error("用户名或密码错误");
        }
    }
}
